<template>
  <a-modal
    width="1000"
    title="销货退货单"
    :visible="visible"
    :confirm-loading="confirmLoading"
    @cancel="visible = false"
    @ok="visible = false"
  >
    <a-form @keyup.enter.native="searchQuery">
      <a-row :gutter="24">
        <a-col :md="6" :sm="24">
          <a-form-item label="单据编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入单据编号" v-model="queryParam.orderNo"></a-input>
          </a-form-item>
        </a-col>
        <a-col :md="6" :sm="24">
          <a-form-item label="单据日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-range-picker
              style="width: 100%"
              v-model="queryParam.createTimeRange"
              format="YYYY-MM-DD"
              :placeholder="['开始时间', '结束时间']"
              @change="onDateChange"
            />
          </a-form-item>
        </a-col>
         <a-col :md="6" :sm="24">
                  <a-form-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <a-select
                      placeholder="请选择客户"
                      showSearch
                      optionFilterProp="children"
                      v-model="queryParam.customerId"
                    >
                      <a-select-option v-for="(item, index) in cusList" :key="index" :value="item.id">
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
        <a-col :md="6" :sm="24">
          <a-form-item label="业务员" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-select placeholder="请选择业务员" showSearch optionFilterProp="children" v-model="queryParam.salesmanId">
              <a-select-option v-for="(item, index) in userList" :key="index" :value="item.id">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="6" :sm="24">
          <a-form-item label="审核员" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-select placeholder="请选择审核员" showSearch optionFilterProp="children" v-model="queryParam.auditorId">
              <a-select-option v-for="(item, index) in userList" :key="index" :value="item.id">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="6" :sm="24">
                  <a-form-item label="审核状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <a-select placeholder="请选择单据状态" v-model="queryParam.isAudited">
                      <a-select-option value="0">未审核</a-select-option>
                      <a-select-option value="1">已审核</a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
        <a-col :md="6" :sm="24">
          <a-form-item label="退款状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <a-select placeholder="请选择单据状态" v-model="queryParam.returnStatus">
                      <a-select-option value="已退款">已退款</a-select-option>
                      <a-select-option value="未退款">未退款</a-select-option>
                      <a-select-option value="部分退款">部分退款</a-select-option>
                    </a-select>
                  </a-form-item>
        </a-col>
        <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
          <a-button type="primary" @click="searchQuery">查询</a-button>
          <a-button style="margin-left: 8px" @click="searchReset">重置</a-button>
        </span>
      </a-row>
    </a-form>

    <a-table size="middle" bordered rowKey="id" :columns="columns" :dataSource="purchaseOrderList">
      <template slot="action" slot-scope="text, record">
        <a slot="action" @click="handleChoose(record)">选择</a>
      </template>
      <span slot="isAudited" slot-scope="text, record">
        <a-badge
          :status="record.isAudited === 1 ? 'success' : 'error'"
          :text="record.isAudited === 1 ? '已审核' : '未审核'"
        />
      </span>
    </a-table>
  </a-modal>
</template>

<script>
import { getSalesReturnOrderOnly } from '@/api/api'
import { findBySelectCusonly, userAll } from '@/api/api'
import { getFormatDate, getPrevMonthFormatDate } from '@/utils/util'
import moment from 'moment'
export default {
  data() {
    return {
      cusList: [],
      userList: [],
      labelCol: {
        span: 5,
      },
      wrapperCol: {
        span: 18,
        offset: 1,
      },
      queryParam: {
        beginTime: getPrevMonthFormatDate(3),
        endTime: getFormatDate(),
        createTimeRange: [moment(getPrevMonthFormatDate(3)), moment(getFormatDate())],
      },
      purchaseOrderList: [],
      visible: false,
      confirmLoading: false,
      columns: [
        { title: '单据编号', dataIndex: 'orderNo', width: 180},
        { title: '客户姓名', dataIndex: 'customerName', width: 100 },
        { title: '销售人员', dataIndex: 'salespersonName', width: 100 },
        { title: '单据日期', dataIndex: 'orderDate', width: 120 },
        {
          title: '是否审核',
          dataIndex: 'isAudited',
          width: 80,
          align: 'center',
          scopedSlots: { customRender: 'customRenderStatus' },
        },
        { title: '退款状态', dataIndex: 'refundStatus', width: 80 },
        { title: '数量', dataIndex: 'quantity', width: 80 },
        { title: '优惠后金额', dataIndex: 'amountAfterDiscount', width: 80 },
        { title: '退款金额', dataIndex: 'reSalesAmount', width: 80 },
        { title: '账户名字', dataIndex: 'settlementAccountName', width: 80 },
        { title: '欠款', dataIndex: 'debt', width: 80 },
        { title: '已退款金额', dataIndex: 'reReceivedAmount', width: 80 },
        { title: '销货单号', dataIndex: 'salesOrderNo', width: 80 },
        { title: '源单据数量', dataIndex: 'sourcePurchaseQuantity', width: 80 },
        { title: '交货方式', dataIndex: 'deliveryMethod', width: 80 },
        { title: '审核人', dataIndex: 'auditorName', width: 80 },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          width: 30,
          scopedSlots: { customRender: 'action' },
        },
      ],
    }
  },
  mounted() {},
  methods: {
    handleChoose(record) {
      this.$emit('choose', record)
      this.visible = false
    },
    show() {
      this.visible = true
      this.initUser()
      this.initSupplier()
      this.searchReset()
    },
    searchQuery() {
      console.log(this.queryParam)
      getSalesReturnOrderOnly(this.queryParam).then((res) => {
        if (res.code === 200 && res.data) {
          this.purchaseOrderList = res.data
        }
      })
    },
    searchReset() {
      this.queryParam = {
        beginTime: getPrevMonthFormatDate(3),
        endTime: getFormatDate(),
        createTimeRange: [moment(getPrevMonthFormatDate(3)), moment(getFormatDate())],
      }
      this.searchQuery()
    },
    onDateChange: function (value, dateString) {
      this.queryParam.beginTime = dateString[0]
      this.queryParam.endTime = dateString[1]
      if (dateString[0] && dateString[1]) {
        this.queryParam.createTimeRange = [moment(dateString[0]), moment(dateString[1])]
      }
    },
    initUser() {
      userAll({}).then((res) => {
        if (res.data) {
          this.userList = res.data
        }
      })
    },
    initSupplier() {
      findBySelectCusonly({}).then((res) => {
        if (res) {
          this.supList = res.data
        }
      })
    },
  },
}
</script>

<style>
</style>